package org.usmile.algorithms.huawei.acm;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

// https://blog.csdn.net/qq_34465338/article/details/128643705
public class _计算网络信号 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int rows = sc.nextInt();
        int cols = sc.nextInt();
        sc.nextLine();

        String[] signalStr = sc.nextLine().split(" ");
        int[][] metric = new int[rows][cols];
        int startRow = 0;
        int startCol = 0;
        int startSignal = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                int signal = Integer.parseInt(signalStr[row * cols + col]);
                if (signal > 0) {
                    startRow = row;
                    startCol = col;
                    startSignal = signal;
                }
                metric[row][col] = signal;
            }
        }
        int targetRow = sc.nextInt();
        int targetCol = sc.nextInt();

        boolean[][] visited = new boolean[rows][cols];
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{startRow, startCol});
        visited[startRow][startCol] = true;
        startSignal--;
        int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        while (!queue.isEmpty()) {
            if (startSignal == 0) {
                break;
            }
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int[] curr = queue.poll();
                int currRow = curr[0];
                int currCol = curr[1];
                for (int[] direction : directions) {
                    int nextRow = currRow + direction[0];
                    int nextCol = currCol + direction[1];
                    if (nextRow >= 0 && nextRow < rows && nextCol >= 0 && nextCol < cols) {
                        if (!visited[nextRow][nextCol] && metric[nextRow][nextCol] != -1) {
                            metric[nextRow][nextCol] = startSignal;
                            visited[nextRow][nextCol] = true;
                            queue.offer(new int[]{nextRow, nextCol});
                        }
                    }
                }
            }
            startSignal--;
        }

        System.out.println(metric[targetRow][targetCol]);
    }
}
